'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
var app = getApp();
exports.default = Page({
    data: {
        agNum: '',
        daili: false,
        current: 0,
        showMask: false,
        activeTabStyle: {
            'color': '#de6360'
        },
        inkBarStyle: {
            'border-bottom': '4rpx solid #de6360',
            'width': '50%',
            'top': '0px'
        },
        tabStyle: {
            'color': '#333a44',
            'border-bottom': '4rpx solid #de6360'
        }
    },
    onLoad: function onLoad() {
        var that = this;
        var token = wx.getStorageSync('__appUserInfo').token;
        that.agentInfos();
    },
    agentInfos: function agentInfos() {
        var that = this;
        var agNum = that.data.agNum;
        var token = wx.getStorageSync('__appUserInfo').token;
        wx.GET(wx.URL.agentinfos, { token: token }).then(function (res) {
            if (res.code == 0) {
                that.setData({
                    agentInfo: res.data
                });
                if (!agNum) {
                    if (res.data.level == 1) {
                        that.agentUserList(0);
                    } else {
                        that.agentUserList(3);
                    }
                }
            }
        });
    },
    agentUserList: function agentUserList(e) {
        var that = this;
        var token = wx.getStorageSync('__appUserInfo').token;
        if (e == 0) {
            //二级代理客户
            var postData = {
                type: 2,
                token: token
            };
        } else if (e == 1) {
            //申请代理客户
            var postData = {
                type: 3,
                token: token
            };
        } else if (e == 2) {
            //普通客户
            var postData = {
                type: 0,
                token: token
            };
        } else if (e == 3) {
            //已下单客户
            var postData = {
                order: 1,
                token: token
            };
        } else if (e == 4) {
            //未下单客户
            var postData = {
                order: 0,
                token: token
            };
        }
        wx.GET(wx.URL.agentlists, postData).then(function (res) {
            if (res.code == 0) {
                that.setData({
                    agNum: 1,
                    agentUserList: res.data
                });
            } else {
                that.setData({
                    agentUserList: ''
                });
            }
        });
    },
    handleChange: function handleChange(e) {
        var index = e.detail.index;
        this.setData({ current: index });
        this.agentUserList(index);
    },
    handleChangeLevel: function handleChangeLevel(e) {
        var index = e.detail.index;
        this.setData({ current: index });
        if (index == 0) {
            this.agentUserList(3);
        } else if (index == 1) {
            this.agentUserList(4);
        }
    },
    handleShowMask: function handleShowMask(e) {
        var show = e.currentTarget.dataset.show;
        this.setData({
            showMask: show
        });
    },
    formSubmit: function formSubmit(e) {
        var that = this;
        var token = wx.getStorageSync('__appUserInfo').token;
        var number = e.detail.value.number;
        if (!number) {
            wx.showModal({
                confirmColor: '#de6360',
                showCancel: false,
                content: '请输入代理返现比例',
                success: function success(res) {}
            });
            return;
        } else if (number < 1) {
            wx.showModal({
                confirmColor: '#de6360',
                showCancel: false,
                content: '返现比例必须等于或者大于1',
                success: function success(res) {}
            });
            return;
        } else if (number > 100) {
            wx.showModal({
                confirmColor: '#de6360',
                showCancel: false,
                content: '返现比例不能高于100',
                success: function success(res) {}
            });
            return;
        } else {
            wx.GET(wx.URL.agentetitb, { proportion: number, token: token }).then(function (res) {
                if (res.code == 0) {
                    that.setData({
                        showMask: false
                    });
                    wx.showToast({ title: '修改成功' });
                    that.agentInfos();
                }
            });
        }
    },
    getAgentApplyTap: function getAgentApplyTap(e) {
        var that = this;
        var uid = e.currentTarget.dataset.id;
        var token = wx.getStorageSync('__appUserInfo').token;
        wx.showModal({
            title: '确认代理成员提示',
            content: '是否确认同意此用户成为您的下级代理？',
            confirmColor: '#de6360',
            success: function success(res) {
                if (res.confirm) {
                    wx.GET(wx.URL.agentsecod, { uid: uid, token: token }).then(function (res) {
                        if (res.code == 0) {
                            wx.showToast({ title: '确认成功' });
                            that.agentUserList(1);
                            that.agentInfos();
                        }
                    });
                }
            }
        });
    },
    getAgentCloseTap: function getAgentCloseTap(e) {
        var that = this;
        var uid = e.currentTarget.dataset.id;
        var token = wx.getStorageSync('__appUserInfo').token;
        wx.showModal({
            title: '取消代理成员提示',
            content: '解除代理客户关系后，此代理以后达成的交易将不会与您分成。是否确认取消此用户的代理资格？',
            confirmColor: '#de6360',
            success: function success(res) {
                if (res.confirm) {
                    wx.GET(wx.URL.agentcance, { uid: uid, token: token }).then(function (res) {
                        if (res.code == 0) {
                            wx.showToast({ title: '确认成功' });
                            that.agentUserList(0);
                            that.agentInfos();
                        }
                    });
                }
            }
        });
    }

});